// https://leetcode.cn/problems/sum-root-to-leaf-numbers/
// Created by ade on 2022/7/22.
//
#include <iostream>
#include <vector>
#include <string>


using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int sum = 0;
    int sumNumbers(TreeNode *root) {
        dfs(root, 0);
        return sum;
    }

    void dfs(TreeNode *root, int tmp) {
        if (root) {
            tmp = tmp * 10 + root->val;
            dfs(root->left, tmp);
            dfs(root->right, tmp);
            if (!root->left && !root->right) {
                sum += tmp;
            }
        }
    }


};

int main() {
    Solution so;
    vector<int> intervals = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1};
    int a = so.sumNumbers(intervals);
    cout << a << endl;
    return 0;
}

